package com.guyaogg.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guyaogg.common.utils.PageUtils;
import com.guyaogg.common.utils.Query;
import com.guyaogg.common.utils.StringUtils;
import com.guyaogg.gulimall.product.dao.AttrGroupDao;
import com.guyaogg.gulimall.product.entity.AttrEntity;
import com.guyaogg.gulimall.product.entity.AttrGroupEntity;
import com.guyaogg.gulimall.product.service.AttrGroupService;
import com.guyaogg.gulimall.product.service.AttrService;
import com.guyaogg.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.guyaogg.gulimall.product.vo.SkuItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper;
        if (catelogId == 0) {
            wrapper = new QueryWrapper<>();
        } else {
            // select * from pms_attr_group where cate catelog_id = ? and (attr_group_id = key or attr_group_name = %key%)
            wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            String trimKey = key.trim();
            wrapper.and(obj ->
                    obj.eq("attr_group_id", trimKey).or().like("attr_group_name", trimKey)
            );
        }
        IPage<AttrGroupEntity> page = page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);

    }

    /**
     * 根据分类 id 查出所有分组以及这些分组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 查询分组信息
        List<AttrGroupEntity> attrGroupEntities = list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        // 查询所有属性
        List<AttrGroupWithAttrsVO> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(group,attrGroupWithAttrsVO);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVO.getAttrGroupId());
            attrGroupWithAttrsVO.setAttrs(attrs);
            return attrGroupWithAttrsVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SkuItemVO.SpuItemBaseAttrVO> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查出当前 spu 所有分组信息，以及当前分组下所有属性对应的值
        return this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
    }


}